import { createConnection, getConnection } from "typeorm";

import { config } from "@src/config";
import { NamingStrategy } from "@src/lib/db/postgres/naming-strategy";

export class DatabaseManager {
  async close(): Promise<void> {
    await getConnection("default").close();
  }

  async connect(entities: Function[]): Promise<void> {
    await createConnection({
      database: config.db.postgres.name,
      entities: entities,
      host: config.db.postgres.host,
      namingStrategy: new NamingStrategy(),
      password: config.db.postgres.password,
      port: config.db.postgres.port,
      synchronize: true, // be extremely careful with this option
      type: "postgres",
      username: config.db.postgres.username
    });
  }
}
